import Vue from 'vue'
import VueRouter from 'vue-router'
import HLLY from '../views/LoginForm.vue' // 引用Login组件
import Home from '../views/HomePage.vue'
// import Welcome from '../components/WelcomePg.vue'
import WebSocket from '../views/debug/WebSocket.vue'
import TcpDebug from '../views/debug/TcpDebug.vue'
import connect from '../views/control/RobotList.vue'
import control from '../views/control/RobotControl.vue'
import configMap from '../views/config/ConfigMap.vue'
import configModel from '../views/config/ConfigModel.vue'
import ThreePg from '../views/three/ThreePg.vue'
Vue.use(VueRouter)

// const routes = []

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: HLLY }, // 定义访问页面的路由地址
    // { path: '/home', component: Home },
    {
      path: '/home',
      component: Home,
      redirect: '/ThreePg',
      children: [       
        { path: '/connect', component: connect },
        { path: '/control', component: control },
        { path: '/map', component: configMap },
        { path: '/model', component: configModel },
        { path: '/WebSocket', component: WebSocket },
        { path: '/TcpDebug', component: TcpDebug },
        { path: '/ThreePg', component: ThreePg }
      ]
    }
  ]
})

// 挂载路由导航守卫
// to是我们跳转的路径，from是来自的路径，next为放行函数
router.beforeEach((to, from, next) => {
  // 如果用户访问登录页，直接放行
  if (to.path === '/login') return next()
  // 从sessionStorage中获取到保存的token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token，强制跳转到登录页面
  if (!tokenStr) return next('/login')
  next()// 有token，直接放行
})

export default router
